package mx.netbrain.dao.impl;

import java.util.List;

import mx.netbrain.dao.PropertiesUserDao;
import mx.netbrain.model.PropertiesUser;
import mx.netbrain.util.HibernateUtil;

import org.apache.log4j.Logger;
import org.hibernate.Session;

public class PropertiesUserDaoImpl implements PropertiesUserDao {

	private static final Logger logger = Logger
			.getLogger(PropertiesUserDaoImpl.class);

	@Override
	public void save(PropertiesUser properties) {
		Session session = HibernateUtil.getSessionFactory().openSession();
		try {
			session.beginTransaction();
			session.persist(properties);
			session.beginTransaction().commit();
		} catch (Exception e) {
			logger.error("Save: " + e.getMessage());
			session.beginTransaction().rollback();
		}
	}

	@Override
	public void update(PropertiesUser properties) {
		Session session = HibernateUtil.getSessionFactory().openSession();
		try {
			session.beginTransaction();
			session.merge(properties);
			session.beginTransaction().commit();
		} catch (Exception e) {
			logger.error("Update: " + e.getMessage());
			session.beginTransaction().rollback();
		}
	}

	@Override
	public void delete(PropertiesUser properties) {
		Session session = HibernateUtil.getSessionFactory().openSession();
		try {
			session.beginTransaction();
			session.merge(properties);
			session.beginTransaction().commit();
		} catch (Exception e) {
			logger.error("Delete: " + e.getMessage());
			session.beginTransaction().rollback();
		}
	}

	@Override
	public PropertiesUser findById(Long id) {
		Session session = HibernateUtil.getSessionFactory().openSession();
		return (PropertiesUser) session.load(PropertiesUser.class, id);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<PropertiesUser> listAll() {
		Session session = HibernateUtil.getSessionFactory().openSession();
		return session.createQuery("from PropertiesUser as properties").list();
	}

}
